import 'package:hrf_app/common/extension/ex_visibility.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

class TextInputEdit extends StatelessWidget {
  /// 文字字符串
  final String? text;
  final Widget? textWidget;
  final bool isCustomText;

  /// 标题
  final String labelText;
  final Color labelTextColor;

  /// 自定义参数 在回调时会原封不动的返回去
  final dynamic customParam;

  /// 是否显示编辑按钮  (editParm && isShowEdit)
  final bool isShowEdit;

  ///编辑文字
  final String editText;
  final Widget? editWidget;
  final bool isCustomEdit;

  /// 第一个时customParam 的内容
  final String? Function(dynamic)? editCallBack;

  /// 编辑的权限串 (editParm && isShowEdit)
  final String editParm;

//是否显示必填写 *
  final bool isMustBeEnter;

  /// 字体大小
  final double size;

  /// padding
  final EdgeInsets? linePadding;
  final BoxDecoration? boxDecoration;

  //  构造函数
  const TextInputEdit(
      {Key? key,
      required this.labelText,
      this.customParam = "",
      this.text,
      this.labelTextColor = Colors.black54,
      this.isMustBeEnter = false,
      this.size = 16,
      this.isShowEdit = false,
      this.editText = "编辑",
      this.editWidget,
      this.textWidget,
      this.editCallBack,
      this.boxDecoration = const BoxDecoration(color: Colors.white),
      this.linePadding = const EdgeInsets.all(8),
      this.editParm = "",
      this.isCustomText = false,
      this.isCustomEdit = false})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
        decoration: boxDecoration,
        padding: linePadding,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            _buildLeft(),
            _buildRight(),
          ],
        ));
  }

  Widget _buildLeft() {
    String mustBeEnter = isMustBeEnter ? "*" : " ";
    return Row(children: [
      Container(
          margin: EdgeInsets.only(top: 4),
          child: Text(
            mustBeEnter,
            style: TextStyle(color: Colors.red, fontSize: size - 2),
          )),
      Text(
        labelText,
        style: TextStyle(fontSize: size, color: labelTextColor),
      ),
    ]);
  }

  Widget _buildRight() {
    Widget textCustomWidget = Text(
      text ?? "暂无",
      style: TextStyle(
        fontSize: size,
      ),
    );
    if (isCustomText) {
      textCustomWidget = textWidget!;
    }

    Widget editCustomWidget = Text(
      editText,
      style: TextStyle(
        fontSize: size - 2,
        color: Colors.blue,
      ),
    );
    if (isCustomEdit) {
      editCustomWidget = editWidget!;
    }
    return Row(children: [
      textCustomWidget,
      const SizedBox(
        width: 10,
      ),
      GestureDetector(
              onTap: () {
                editCallBack!(customParam);
              },
              behavior: HitTestBehavior.opaque,
              excludeFromSemantics: false,
              dragStartBehavior: DragStartBehavior.start,
              child: editCustomWidget)
          .hasPermAndVisible(hasPerm: editParm, visible: isShowEdit),
    ]);
  }
}
